package cc.z8g.browser.search.suggestions

import cc.z8g.browser.database.SearchSuggestion
import cc.z8g.browser.utils.sp.UserSp
import cc.z8g.browser.search.BaseSearchEngine
import io.reactivex.rxjava3.core.Single

/**
 * The model that provides the search engine based
 * on the user's preference.
 */
object SearchEngineProvider {

    /** A search suggestions repository that doesn't fetch any results. */
    private class NoOpSuggestionsRepository : SuggestionsRepository {
        override fun resultsForSearch(rawQuery: String): Single<List<SearchSuggestion>> = Single.just(emptyList())
    }
    /**
     * Provide the [SuggestionsRepository] that maps to the user's current preference.
     */
    internal fun provideSearchSuggestions(isIncognito: Boolean): SuggestionsRepository {
        if (isIncognito) {
            return NoOpSuggestionsRepository()
        }
        return when (UserSp.getSearchSuggestion()) {
            SearchConst.SUGGESTION_BAIDU -> BaiduSuggestionsModel()
            SearchConst.SUGGESTION_GOOGLE -> GoogleSuggestionsModel()
            else -> NoOpSuggestionsRepository()
        }
    }

    /**
     * Provide the [BaseSearchEngine] that maps to the user's current preference.
     */
    fun provideSearchEngine(): BaseSearchEngine = UserSp.getSearchEngine()

}
